//
//  Monster.cpp
//  fingeradvanture
//
//  Created by 8089 on 16/5/13.
//
//

#include "Monster.h"

Monster::Monster()
{
    
}

Monster::~Monster()
{
    
}

Monster* Monster::create(b2World *world, MonsterType type)
{
    Monster* pRet = new Monster();
    if(pRet && pRet->init(world,type))
    {
        pRet->autorelease();
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}

bool Monster::init(b2World *world, MonsterType type)
{
    SpriteFrameCache* cache =SpriteFrameCache::getInstance();
    cache->addSpriteFramesWithFile("elements.plist",Director::getInstance()->getTextureCache()->addImage("elements.png"));
//
    
    const std::string str = getMosterTypeName(type);
    if(str.find("_") != str.rfind("_"))
        monster = Sprite::createWithSpriteFrameName(str.substr(0, str.length() - 2) +".png");
    else
        monster = Sprite::createWithSpriteFrameName(str + ".png");
    if (monster)
    {
        b2BodyDef bodyDef;
        bodyDef.type = b2_dynamicBody;
        
        bodyDef.position.Set(monster->getPositionX()/PTM_RATIO, monster->getPositionY()/PTM_RATIO);
        bodyDef.userData = monster;
        b2Body *body = world->CreateBody(&bodyDef);
        if (body)
        {
            GB2ShapeCache *sc = GB2ShapeCache::sharedGB2ShapeCache();
            if (sc)
            {
                sc->addFixturesToBody(body, str.c_str());
                monster->setAnchorPoint(sc->anchorPointForShape(str.c_str()));
                return true;
            }
        }
    }
    
    return false;
}

const std::string Monster::getMosterTypeName(MonsterType type)
{
    const char* monsterName[]
    {
        "monster_1",
        "monster_2",
        "monster_3",
        "monster_4",
        "monster_5",
        "monster_6",
        "monster_5_1",  //六边形组队成员
        "monster_5_2",  //四边形组队成员
        "monster_2_1",
        "monster_2_2",
        "monster_4_1",  //六边形组队成员
        "monster_4_2",  //四边形组队成员
        "monster_4_3",  //十字形组队成员
        "spikes_1",
        "spikes_2",
        "spikes_3",
        "spikes_4",
    };
    
    return monsterName[type - 1];
}
